import CryptoJS from 'crypto-js'
const SECRET_KEY = import.meta.env.VITE_CRYPTO_KEY || 'e9d7b8f4a12c3d5e6f7a9b8c5d2e1f0c3b6a9d8e7f4c1b2a5d8e3f6a9c4b7'

export const encryptData = (data: string) => {
  return CryptoJS.AES.encrypt(data, SECRET_KEY).toString()
}

export const decryptData = (ciphertext: string) => {
  const bytes = CryptoJS.AES.decrypt(ciphertext, SECRET_KEY)
  return bytes.toString(CryptoJS.enc.Utf8)
}

export const encryptKey = CryptoJS.enc.Utf8.parse('supersonic@2024');

export const encryptPassword = (password: string, key = encryptKey) => {
  if (!password) {
    return password;
  }
  const srcs = CryptoJS.enc.Utf8.parse(password);
  const encrypted = CryptoJS.AES.encrypt(srcs, key || encryptKey, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  });
  return encrypted.toString();
};